package gui.card;

import gui.alert;
import pojo.Card;
import service.cardService;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class del_Card {
    private JTextField textField1;
    private JButton 按id查询Button;
    private JTextField textField2;
    private JButton 按姓名查找Button;
    private JPanel query;
    private JTextArea result;
    private JButton 会员禁用Button;
    private Card card_tmp;

    public del_Card() {
        按id查询Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                Card card = cardService.queryByid(Integer.parseInt(textField1.getText()));
                if (card != null) {
                    card_tmp = card;
                    result.setText(card.toString());
                } else {
                    result.setText("无结果");
                }

            }
        });
        按姓名查找Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                Card card = cardService.queryByName(textField2.getText());
                if (card != null) {
                    card_tmp = card;
                    result.setText(card.toString());
                } else {
                    result.setText("无结果");
                }
            }
        });
        会员禁用Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                if (cardService.deleteByid((int) card_tmp.getId()) == 1) {
                    alert.show("退卡成功");
                } else {
                    alert.show("退卡失败");
                }

            }
        });
    }

    public static void show() {
        JFrame frame = new JFrame("禁用卡片");
        frame.setContentPane(new del_Card().query);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
